package main

import (
	"ProjectOne/projectDemo/CustomerManagementSystem/model"
	"ProjectOne/projectDemo/CustomerManagementSystem/service"
	"fmt"
)

type customerView struct {
	customerService *service.CustomerService
}

//显示所有客户信息
func (this *customerView) list() {
	//获取当前所有客户信息
	customers := this.customerService.List()
	//显示
	fmt.Println("--------------客户列表----------------")
	fmt.Println("编号  姓名   性别  年龄  电话     邮箱     ")
	for i := 0; i < len(customers); i++ {
		fmt.Println(customers[i].GetInfo())
	}
	fmt.Println("--------------客户列表完成--------------")
}

//添加客户
//得到用户输入信息，构建新的客户，完成添加
func (this *customerView) addCustomer() {
	name := ""
	age := 0
	gender := ""
	phone := ""
	email := ""
	fmt.Println("--------------添加客户--------------")
	fmt.Println("姓名：")
	_, _ = fmt.Scanln(&name)
	fmt.Println("性别:")
	_, _ = fmt.Scanln(&gender)
	fmt.Println("年龄:")
	_, _ = fmt.Scanln(&age)
	fmt.Println("电话：")
	_, _ = fmt.Scanln(&phone)
	fmt.Println("邮箱:")
	_, _ = fmt.Scanln(&email)
	customer := model.CreateNewCustomer(name, gender, age, phone, email)
	if this.customerService.Add(*customer) {
		fmt.Println("-------------添加完成--------------------")
	} else {
		fmt.Println("-------------添加失败--------------")
	}
}
func (this *customerView) modifyCustomer() bool {
	name := ""
	age := 0
	gender := ""
	phone := ""
	email := ""
	field := 0
	customerNum := 0
	fmt.Println("请选择待修改的和客户编号")
	fmt.Scanln(&customerNum)
	if this.customerService.FindById(customerNum) == -1 {
		fmt.Println("没有该用户")
		return false
	}
	customer := this.customerService.Modify(customerNum)

	for {
		loop := true
		fmt.Println("请选择要修改的字段")
		fmt.Println("1.姓名：")
		fmt.Println("2.性别:")
		fmt.Println("3.年龄:")
		fmt.Println("4.电话：")
		fmt.Println("5.邮箱:")
		fmt.Println("6.退出:")
		fmt.Scanln(&field)
		switch field {
		case 1:
			fmt.Printf("旧姓名为%v  新姓名为:", customer.Name)
			_, _ = fmt.Scanln(&name)
			customer.Name = name
		case 2:
			fmt.Printf("旧性别为%v  新性别为:", customer.Gender)
			_, _ = fmt.Scanln(&gender)
			customer.Gender = gender
		case 3:
			fmt.Printf("旧年龄为%v  新的年龄为：", customer.Age)
			_, _ = fmt.Scanln(&age)
			customer.Age = age
		case 4:
			fmt.Printf("旧电话号码为%v   新的电话为：", customer.Phone)
			_, _ = fmt.Scanln(&phone)
			customer.Phone = phone
		case 5:
			fmt.Printf("旧邮箱号码为%v  新的邮箱为：", customer.Email)
			_, _ = fmt.Scanln(&email)
			customer.Email = email
		case 6:
			loop = false
		}
		if loop == false {
			break
		}
	}
	return true
}
func (this *customerView) deleteCustomer() {
	customerId := 0
	deleteSign := ""
	fmt.Println("----------------删除客户----------------")
	fmt.Println("请选择删除客户编号")
	_, _ = fmt.Scanln(&customerId)
	fmt.Println("确认是否删除(y/n):")
	fmt.Scanln(&deleteSign)
	if deleteSign == "y" || deleteSign == "Y" {
		if this.customerService.Delete(customerId) == true {
			fmt.Println("----------------删除完成----------------")
		} else {
			fmt.Println("--------------删除失败，输入的id不存在----------------")
		}
	}
	if deleteSign == "n" || deleteSign == "N" {
		return
	}
}

//显示主菜单
func (this *customerView) showMainMenu() {
	var choice int8
	var flag bool = true
	for {
		fmt.Println("---------------客户信息管理系统---------------")
		fmt.Println("                1.添加客户                      ")
		fmt.Println("                2.修改客户                      ")
		fmt.Println("                3.删除客户                      ")
		fmt.Println("                4.展示客户列表                  ")
		fmt.Println("                5.退  出                       ")
		fmt.Println("------------------------------------------------")
		fmt.Println("请选择(1-5)")
		_, _ = fmt.Scanln(&choice)
		switch choice {
		case 1:
			this.addCustomer()
		case 2:
			this.modifyCustomer()
		case 3:
			this.deleteCustomer()
		case 4:
			this.list()
		case 5:
			flag = false

		default:
			fmt.Println("请输入正确的数字")
		}
		if flag == false {
			break
		}
	}
	fmt.Println("您已退出")
}
func main() {
	cv := customerView{}
	cv.customerService = service.NewCustomerService()
	cv.showMainMenu()
}
